﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1010PhotoManagement
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        //指向照片管理对象的指针
        private ControlClass controlObject = null;
        //重新显示被选中的照片
        public void RefreshAll(PictBaseClass _pbc)
        {
            if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
            pictureBox1.Image = _pbc.GetBitmap();
            textBox1.Text = "照片名称：" + _pbc.PictName + "\r\n"
                + "拍照时间：" + _pbc.PictDate.ToString("yyyy年MM月dd日") + "\r\n"
                + "照片简介：" + "\r\n" + _pbc.PictIntro;
        }
        //构造方法
        public MainForm(ControlClass _cc)
        {
            InitializeComponent();
            //控制类对象
            controlObject = _cc;
            //设置ListBox的快捷菜单
            listBox1.ContextMenuStrip = contextMenuStrip1;
            //图像按原有比例尽量充满绘图区
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }
       
       
        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AMForm amf = new AMForm();
            amf.Text = "增加照片";
            if (DialogResult.OK == amf.ShowDialog())
            {
                //用户点击AMForm的“确定”按钮并关闭窗体返回后，
                //创建新的照片对象，然后加入列表
                PictBaseClass pbc = new PictBaseClass(amf.PictName, amf.PictDate,
                                                        amf.PictIntro, amf.PictPath);
                controlObject.pmc.AddPict(pbc);
                //更新ListBox1的内容
                int index = listBox1.Items.Add(pbc.PictName);
                //选中新增照片，将触发SelectedIndexChanged事件
                //进而导致listBox1_SelectedIndexChanged方法执行
                listBox1.SetSelected(index, true);
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            if (index < 0) return;
            AMForm amf = new AMForm();
            amf.Text = "修改照片信息";
            //将当前选中照片的详细信息传入AMForm对象
            PictBaseClass pbc = controlObject.pmc.GetPict(index);
            amf.PictName = pbc.PictName;
            amf.PictIntro = pbc.PictIntro;
            amf.PictDate = pbc.PictDate;
            amf.PictPath = pbc.PictPath;
            if (DialogResult.OK == amf.ShowDialog())
            {
                pbc = new PictBaseClass(amf.PictName, amf.PictDate,
                                        amf.PictIntro, amf.PictPath);
                controlObject.pmc.ModiFyPict(index, pbc);
                listBox1.Items[index] = pbc.PictName;
                //选中被修改的照片，导致listBox1_SelectedIndexChanged方法执行
                listBox1.SetSelected(index, true);
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            if (index < 0) return;
            if (index == listBox1.Items.Count - 1)//如果是最后一张
            {
                if (index == 0)//如果是最后一张且仅剩一张
                {
                    controlObject.pmc.DeletePict(index);
                    listBox1.Items.RemoveAt(index);
                    pictureBox1.Image = null;
                    return; //删除照片，但不更新界面
                }
                else
                {
                    controlObject.pmc.DeletePict(index);
                    listBox1.Items.RemoveAt(index);
                    listBox1.SetSelected(listBox1.Items.Count - 1, true);
                    return; //删除最后一张，且选择当前最后一张
                }
            }
            else //如果不是最后一张
            {
                controlObject.pmc.DeletePict(index);
                listBox1.Items.RemoveAt(index);
                listBox1.SetSelected(index, true);
                return; //删除当前照片，且选择下一张
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            if (listBox1.SelectedIndex > -1)
                controlObject.SetWinContent(listBox1.SelectedIndex, this);

        }
    }
}
